package com.technology.education.ui.common.pay

import androidx.lifecycle.MutableLiveData
import com.technology.education.data.SpKeys
import com.technology.education.data.model.PayBodyModel
import com.technology.education.ext.toast
import com.technology.education.network.apiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.IntObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request

/**
 *@author:create by BZY
 *Date: 2022/3/23 08
 */
class PayViewModel : BaseViewModel() {
    /**
     * 支付方式
     */
    val payType = IntObservableField(2)
    val payString = StringObservableField("立即支付")
    var orderId = ""
    val result = MutableLiveData<Any>()

    /**
     * 支付类型
     */
    var orderType = 0


    override fun getData() {
        super.getData()
        request(
            {
                if (payType.get() == 1) {
                    when (orderType) {
                        SpKeys.PAY_TYPE_DEPOSIT -> apiService.payDepositOrder(
                            hashMapOf(
                                "oid" to orderId,
                                "payChannel" to payType.get()
                            )
                        )
                        SpKeys.PAY_TYPE_CHARGE -> apiService.payChargeOrder(
                            hashMapOf(
                                "oid" to orderId,
                                "payChannel" to payType.get()
                            )
                        )
                        else -> apiService.payChargeOrder(
                            hashMapOf(
                                "oid" to orderId,
                                "payChannel" to payType.get()
                            )
                        )
                    }
                } else {
                    when (orderType) {
                        SpKeys.PAY_TYPE_DEPOSIT -> apiService.payDepositOrderWx(
                            hashMapOf(
                                "oid" to orderId,
                                "payChannel" to payType.get()
                            )
                        )
                        SpKeys.PAY_TYPE_CHARGE -> apiService.payChargeOrderWx(
                            hashMapOf(
                                "oid" to orderId,
                                "payChannel" to payType.get()
                            )
                        )
                        else -> apiService.payChargeOrderWx(
                            hashMapOf(
                                "oid" to orderId,
                                "payChannel" to payType.get()
                            )
                        )
                    }
                }

            },
            success = {
                result.postValue(it)
            }, error = {
                toast(it.errorMsg)
            }, isShowDialog = true
        )
    }

    fun toggleType(type: Int) {
        payType.set(type)
    }
}